function y= penalty_bc_shock(q1)
% function y= penalty_bc_shock(q1)
% q1: rotation vector for IRF
% Compute penalty function for business cycle shock

% Based on replication codes for Dario Caldara and Christophe Kamps (2017), The Analytics of SVARs: A
% Unified Framework to Measure Fiscal Multipliers, Review of Economic Studies (2017) 84, 1015–1040
%
% Copyright: 2017 Dario Caldara and Christophe Kamps
% Copyright: 2020-2023 Benjamin Born, Francesco D'Ascanio, Gernot J. Mueller, Johannes Pfeifer

global IR; %IRFs to Cholesky factor
global ssigma; %standard deviation of reduced form residuals
global nper; %number of periods for penalty


index=[1 3]; %index for variables, T=1 and Y= 3
x=zeros(nper,size(index,2));

for i=1:size(index,2)
    for h=1:nper
        x(h,i)=-(1/ssigma(index(i)))*squeeze(IR(h,index(i),:))'*q1; %do rotation of reduced form IRFs
    end
end

y=0;
for i=1:size(index,2)
    for h=1:nper
        if (x(h,i)>0)
            y=100*x(h,i)+ y; %violates restriction due to minus above
        else
            y=x(h,i) + y;
        end
    end
end